<script setup lang="ts">
import { computed} from 'vue';
import { $t } from '@/locales';
import { useAntdForm, useFormRules } from '@/hooks/common/form';
import { userGenderOptions } from '@/constants/business';
import { translateOptions } from '@/utils/common';
import { deviceTypeEnum } from '@/enum';

defineOptions({
  name: 'UserSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const { formRef, validate, resetFields } = useAntdForm();

const model = defineModel<any>('model', { required: true });
const deviceCategoryList = defineModel<any>('deviceCategoryList', { required: true });
const getewayData = defineModel<any>('getewayData', { required: true });
const hostData = defineModel<any>('hostData', { required: true });
const farmData = defineModel<any>('farmData', { required: true });

type RuleKey = Extract<keyof Api.SystemManage.UserSearchParams, 'userEmail' | 'userPhone'>;

const rules = computed<Record<RuleKey, App.Global.FormRule>>(() => {
  const { patternRules } = useFormRules(); // inside computed to make locale reactive

  return {
    userEmail: patternRules.email,
    userPhone: patternRules.phone
  };
});

async function reset() {
  await resetFields();
  emit('reset');
  emit('search');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <ACard :bordered="false" class="card-wrapper">
    <AForm
      ref="formRef"
      :model="model"
      :rules="rules"
      :label-col="{
        span: 5,
        md: 7
      }"
    >
      <ARow :gutter="[16, 16]" wrap>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="设备名称" name="assertName" class="m-0">
            <AInput v-model:value="model.deviceName" placeholder="请输入设备名称" />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="设备编码" name="deviceNo" class="m-0">
            <AInput v-model:value="model.deviceNo" placeholder="请输入设备编码" />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="设备类型" name="deviceType" class="m-0">
            <ASelect
              v-model:value="model.deviceType"
              placeholder="请选择设备类型"
              :options="deviceTypeEnum"
              clearable
            />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="设备分类" name="categoryCode" class="m-0">
            <ASelect
              v-model:value="model.categoryCode"
              placeholder="请选择设备分类"
              :options="deviceCategoryList"
              :field-names="{ label: 'categoryName', value: 'categoryCode', options: 'options' }"
              clearable
            />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="网关名称" name="collectionNo" class="m-0">
            <ASelect
              v-model:value="model.collectionNo"
              placeholder="请选择网关名称"
              :field-names="{ label: 'assetName', value: 'collectionNo', options: 'options' }"
              :options="getewayData"
              clearable
            />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="终端名称" name="hostNo" class="m-0">
            <ASelect
              v-model:value="model.hostNo"
              placeholder="请选择终端名称"
              :options="hostData"
              :field-names="{ label: 'hostName', value: 'hostNo', options: 'options' }"
              clearable
            />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="养殖池" name="farmId" class="m-0">
            <ASelect
              v-model:value="model.farmId"
              placeholder="请选择养殖池"
              :field-names="{ label: 'name', value: 'id', options: 'options' }"
              :options="farmData"
              clearable
            />
          </AFormItem>
        </ACol>
        <div class="flex-1">
          <AFormItem class="m-0">
            <div class="w-full flex-y-center justify-end gap-12px">
              <AButton @click="reset">
                <template #icon>
                  <icon-ic-round-refresh class="align-sub text-icon" />
                </template>
                <span class="ml-8px">重置</span>
              </AButton>
              <AButton type="primary" ghost @click="search">
                <template #icon>
                  <icon-ic-round-search class="align-sub text-icon" />
                </template>
                <span class="ml-8px">搜索</span>
              </AButton>
            </div>
          </AFormItem>
        </div>
      </ARow>
    </AForm>
  </ACard>
</template>

<style scoped></style>
